package com.talkingdata.talkingdata_sdk_plugin;

import android.content.Context;

import androidx.annotation.NonNull;

import com.tendcloud.tenddata.TalkingDataGender;
import com.tendcloud.tenddata.TalkingDataProfile;
import com.tendcloud.tenddata.TalkingDataProfileType;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.tendcloud.tenddata.TalkingDataSDKConfig;

import java.util.List;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;

public class TalkingDataSDKPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private static Context mContext;

  public static void registerWith(PluginRegistry.Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "talkingdata_sdk_plugin");
    channel.setMethodCallHandler(new TalkingDataSDKPlugin());
  }

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "talkingdata_sdk_plugin");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  public static void setContext(Context ctx) {
    mContext = ctx;
  }

  public static Context getContext() {
    return mContext;
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method) {
      case "initSDK":
      case "init":
        TalkingDataSDK.initSDK(mContext,(String) call.argument("appID"),(String) call.argument("channelID"),(String) call.argument("custom"));
        break;

      case "startA":
        TalkingDataSDK.startA(mContext);
        break;
      case "setConfig":
        TalkingDataSDKConfig config = new TalkingDataSDKConfig();
        boolean imei = callTransBool(call,"IMEIAndMEID");
        boolean mac = callTransBool(call,"Mac");
        boolean appList = callTransBool(call,"AppList");
        boolean location =callTransBool(call,"Location");
        config.setAppListEnabled(appList)
                .setLocationEnabled(location)
                .setMACEnabled(mac)
                .setIMEIAndMEIDEnabled(imei);
        TalkingDataSDK.setConfig(config);
        break;
      case "getDeviceID":
        result.success(TalkingDataSDK.getDeviceId(mContext));
        break;
      case "getOAID":
        result.success(TalkingDataSDK.getOAID(mContext));
        break;
      case "onPageBegin":
        TalkingDataSDK.onPageBegin(mContext, (String) call.argument("pageName"));
        break;
      case "onPageEnd":
        TalkingDataSDK.onPageEnd(mContext, (String) call.argument("pageName"));
        break;

      case "onEvent":
        Map paramWith = null;
        if (call.argument("params") instanceof Map) {
          paramWith = call.argument("params");
        }
        TalkingDataSDK.onEvent(mContext,
                (String) call.argument("eventID"),
                paramWith
        );
        break;
      case "setGlobalKV":
        String globalKey = call.argument("key");
        Object globalValue = call.argument("value");
        TalkingDataSDK.setGlobalKV(globalKey, globalValue);
        break;
      case "removeGlobalKV":
        String key = call.argument("key");
        TalkingDataSDK.removeGlobalKV(key);
        break;
      case "onRegister":
        String profileId = call.argument("profileId");
        String invitationCode = call.argument("invitationCode");
        TalkingDataProfile profile = TalkingDataProfile.createProfile();
        profile.setType(TalkingDataProfileType.valueOf((String)call.argument("type")));
        profile.setName((String)call.argument("name"));
        profile.setGender(TalkingDataGender.valueOf((String)call.argument("gender")));
        profile.setAge((int)call.argument("age"));
        profile.setProperty1((Object)call.argument("property1"));
        profile.setProperty2((Object)call.argument("property2"));
        profile.setProperty3((Object)call.argument("property3"));
        profile.setProperty4((Object)call.argument("property4"));
        profile.setProperty5((Object)call.argument("property5"));
        profile.setProperty6((Object)call.argument("property6"));
        profile.setProperty7((Object)call.argument("property7"));
        profile.setProperty8((Object)call.argument("property8"));
        profile.setProperty9((Object)call.argument("property9"));
        profile.setProperty10((Object)call.argument("property10"));
        TalkingDataSDK.onRegister(profileId, profile, invitationCode);
        break;
      case "onLogin":
        TalkingDataProfile profileLogin = TalkingDataProfile.createProfile();
        profileLogin.setType(TalkingDataProfileType.valueOf((String)call.argument("type")));
        profileLogin.setName((String)call.argument("name"));
        profileLogin.setGender(TalkingDataGender.valueOf((String)call.argument("gender")));
        profileLogin.setAge((int)call.argument("age"));
        profileLogin.setProperty1((Object)call.argument("property1"));
        profileLogin.setProperty2((Object)call.argument("property2"));
        profileLogin.setProperty3((Object)call.argument("property3"));
        profileLogin.setProperty4((Object)call.argument("property4"));
        profileLogin.setProperty5((Object)call.argument("property5"));
        profileLogin.setProperty6((Object)call.argument("property6"));
        profileLogin.setProperty7((Object)call.argument("property7"));
        profileLogin.setProperty8((Object)call.argument("property8"));
        profileLogin.setProperty9((Object)call.argument("property9"));
        profileLogin.setProperty10((Object)call.argument("property10"));
        TalkingDataSDK.onLogin((String) call.argument("profileId"), profileLogin);
        break;
      case "onProfileUpdate":
        TalkingDataProfile profileUpdate = TalkingDataProfile.createProfile();
        profileUpdate.setType(TalkingDataProfileType.valueOf((String)call.argument("type")));
        profileUpdate.setName((String)call.argument("name"));
        profileUpdate.setGender(TalkingDataGender.valueOf((String)call.argument("gender")));
        profileUpdate.setAge((int)call.argument("age"));
        profileUpdate.setProperty1((Object)call.argument("property1"));
        profileUpdate.setProperty2((Object)call.argument("property2"));
        profileUpdate.setProperty3((Object)call.argument("property3"));
        profileUpdate.setProperty4((Object)call.argument("property4"));
        profileUpdate.setProperty5((Object)call.argument("property5"));
        profileUpdate.setProperty6((Object)call.argument("property6"));
        profileUpdate.setProperty7((Object)call.argument("property7"));
        profileUpdate.setProperty8((Object)call.argument("property8"));
        profileUpdate.setProperty9((Object)call.argument("property9"));
        profileUpdate.setProperty10((Object)call.argument("property10"));
        TalkingDataSDK.onProfileUpdate(profileUpdate);
        break;
      case "onCreateCard":
        TalkingDataSDK.onCreateCard((String) call.argument("profile"),
                (String) call.argument("method"),
                (String) call.argument("content"));
        break;
      case "onReceiveDeepLink":
        TalkingDataSDK.onReceiveDeepLink((String) call.argument("link"));
        break;
      case "onFavorite":
        TalkingDataSDK.onFavorite((String) call.argument("category"),
                (String) call.argument("content"));
        break;
      case "onShare":
        TalkingDataSDK.onShare((String) call.argument("profile"),
                (String) call.argument("content"));
        break;
      case "onPunch":
        TalkingDataSDK.onPunch((String) call.argument("profile"),
                (String) call.argument("punchId"));
        break;
      default:
        result.notImplemented();
        break;
    }
  }
  private int mapTransInt(Map map, String dsc) {
    if (map.get(dsc) != null) {
      return (int) map.get(dsc);
    } else {
      return 0;
    }
  }

  private int callTransInt(MethodCall call, String dsc) {
    if (call.argument(dsc) != null) {
      return (int) call.argument(dsc);
    } else {
      return 0;
    }
  }

  private boolean callTransBool(MethodCall call,String dsc){
    if(call.argument(dsc) != null){
      return call.argument(dsc);
    }else{
      return true;
    }
  }
}
